package info.empathymobile.forecast.testenv;

import info.empathymobile.forecast.testenv.beans.TestBaseData;
import info.empathymobile.forecast.testenv.database.DatabaseHelper;
import info.empathymobile.forecast.testenv.database.OrmLiteBase;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.j256.ormlite.android.apptools.OrmLiteBaseActivity;

public class MainActivity extends OrmLiteBaseActivity<DatabaseHelper> implements OrmLiteBase<DatabaseHelper>, TestLogOutput {
	
	private TextView testLogOutput;
	private ForecastTestController forecastTestController;
	private ServerController serverController;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		this.serverController = new ServerController(this);
		
		setViewReferences();
		setViewsCallbacks();
		this.forecastTestController = new ForecastTestController(this);
	}

	private void setViewReferences() {
		testLogOutput = (TextView) findViewById(R.id.consoleOutPutTxtView);
	}

	private void setViewsCallbacks() {
		((Button)findViewById(R.id.startForecastTestBtn)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onStartForecast();
			}
		});
		((Button)findViewById(R.id.clearOutputBtn)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onClearOutput();
			}
		});
		((Button)findViewById(R.id.pullDataBtn)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onPullData();
			}
		});
		((Button)findViewById(R.id.pushTestLogsBtn)).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				onPushTestsLogs();
			}
		});
	}

	protected void onPullData() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				List<TestBaseData> testBaseDataParams = new ArrayList<TestBaseData>();
				String[] configuredTestBaseDataUserUUID = getConfiguredTestBaseDataUserUUID();
				
				for (String userUUID : configuredTestBaseDataUserUUID) {
					testBaseDataParams.add(new TestBaseData(
						userUUID, 
						getConfiguredForecastThreshold(), 
						getConfiguredForecastDaysAgo(), 
						getConfiguredTestDataType()
					));
				}
				if(!testBaseDataParams.isEmpty()){
					serverController.getTestBaseData(testBaseDataParams);
					printLogMsg("Data Pulled!");
				}
				else {
					printLogMsg("Nenhum UserUUID foi selecionado!");
				}
			}
		}).start();
	}

	protected void onPushTestsLogs() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				printLogMsg("Sending Test Logs to Servers");
				
				int notSyncronized = new ServerController(MainActivity.this).sendTestLogsToServer();
				String msg = "All Tests Syncronized!";
				if(notSyncronized > 0){
					msg = notSyncronized + " Test Logs not syncronized!";
				}
				
				printLogMsg(msg);
			}
		}).start();
	}

	protected void onClearOutput() {
		testLogOutput.setText("");
	}

	protected void onStartForecast() {
		List<TestBaseData> testBaseDataParams = new ArrayList<TestBaseData>();
		String[] configuredTestBaseDataUserUUID = getConfiguredTestBaseDataUserUUID();
		
		for (String userUUID : configuredTestBaseDataUserUUID) {
			testBaseDataParams.add(new TestBaseData(
				userUUID, 
				getConfiguredForecastThreshold(), 
				getConfiguredForecastDaysAgo(), 
				getConfiguredTestDataType()
			));
		}
		if(!testBaseDataParams.isEmpty()){
			forecastTestController.startForecastTest(
					testBaseDataParams, 
					getConfiguredAlgorithm(), 
					getConfiguredForecastHorizon()
			);
		}
		else {
			printLogMsg("Nenhum UserUUID foi selecionado!");
		}
	}

	private int getConfiguredForecastHorizon() {
		return this.getResources().getInteger(R.integer.base_data_horizon);
	}
	
	private int getConfiguredForecastThreshold() {
		return this.getResources().getInteger(R.integer.base_data_threshold);
	}
	
	private int getConfiguredForecastDaysAgo() {
		return this.getResources().getInteger(R.integer.base_data_days_ago);
	}

	private String getConfiguredTestDataType() {
		return this.getResources().getString(R.string.base_data_type);
	}

	private String getConfiguredAlgorithm() {
		return this.getResources().getString(R.string.test_algorithm);
	}
	
	private String[] getConfiguredTestBaseDataUserUUID() {
		String testDataType = getConfiguredTestDataType();
		if(testDataType.equals(TestBaseData.BATTERY_DATA)){
			return this.getResources().getStringArray(R.array.base_data_battery_users_uuids);
		}
		else if(testDataType.equals(TestBaseData.CONNECTIVITY_3G_DATA)){
			return this.getResources().getStringArray(R.array.base_data_connectivity_3G_uuids);
		}
		else if(testDataType.equals(TestBaseData.CONNECTIVITY_WIFI_DATA)){
			return this.getResources().getStringArray(R.array.base_data_connectivity_WIFI_uuids);
		}
		return null;
	}

	@Override
	public void printLogMsg(final String logMsg) {
		runOnUiThread(new Runnable() {
			
			@Override
			public void run() {
				testLogOutput.setText(testLogOutput.getText() + logMsg + "\n");
			}
		});
	}
}
